//
//  SearchTableViewCell.swift
//  ZNZXSuperMarket
//
//  Created by 四川中农 on 2018/8/1.
//  Copyright © 2018年 四川中农. All rights reserved.
//

import UIKit



class SearchTableViewCell: UITableViewCell {

    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var clearButton: UIButton!
    @IBOutlet weak var collectionViewHeight: NSLayoutConstraint!
    @IBOutlet weak var collectionView: UICollectionView!
    
    var clearClosure:TouchBlock!
    
    var backSeleValue:BackStringValue!
    
    var dataArray = [Any]()
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.itemSize = CGSize.init(width: (UIScreen.ScreenWidth - 69)/4, height: 30)
        flowLayout.sectionInset = UIEdgeInsetsMake(15, 12, 15, 12)
        flowLayout.minimumLineSpacing = 10
        flowLayout.minimumInteritemSpacing = 15
        
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        self.collectionView.setCollectionViewLayout(flowLayout, animated: true)
        
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    
    // MARK:清除
    @IBAction func clearButtonMethod(_ sender: UIButton) {
        if self.clearClosure != nil {
            self.clearClosure()
        }
    }
    
    func updateCollectionViewHeight(array:[Any]) {
        self.dataArray = array
        let count = array.count
        let line = count % 4 == 0 ? count / 4 : count / 4 + 1
        let height = 30 * line + 10 * (line - 1) + 30
        self.collectionViewHeight.constant = CGFloat(height)
        self.collectionView.reloadData()
    }
}

extension SearchTableViewCell:UICollectionViewDelegate,UICollectionViewDataSource {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HotSearchCollectionViewCell", for: indexPath) as! HotSearchCollectionViewCell
        let value = self.dataArray[indexPath.row]
        cell.titleLabel.text = value is HotSearchValueModel ? (value as! HotSearchValueModel).keyword : value as! String
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let value = self.dataArray[indexPath.row]
        if value is HotSearchValueModel  {
            if self.backSeleValue != nil {
                self.backSeleValue((value as! HotSearchValueModel).keyword)
            }
        }else{
            if self.backSeleValue != nil {
                self.backSeleValue(value as! String)
            }
        }
    }
}





















